package com.flatfish.acvsystem.uitl;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author luohuida
 * @Time   Mar 7, 2015
 * @Email  luo.archer@gmail.com
 * @Description <b>实现文件的下载</b>
 */
public class DownloadFileUtils {
     /**
     * @author  luohuida
     * @time    Mar 7, 2015
     * @param  fileName
     * @param  fileURL
     * @return 保存的路径名
     * @Description <b>根据url下载文件</b>
     */
    public static String downloadFileByURL(String fileName, String fileURL) {
    	if(null==fileName || null==fileURL)  return null;
    	
        byte[] fileBytes = getFileBytesByURL(fileURL);
        if (null != fileBytes && fileBytes.length > 0) {
            System.out.println("读取到：" + fileBytes.length + " 字节");
            return writeFileToDisk(fileBytes, fileName);
        } else {
            System.out.println("没有从该连接获得内容");
            return null;
        }

    }
    
    
     /**
     * @author  luohuida
     * @time    Mar 7, 2015
     * @param strUrl 网络连接地址
     * @return
     * @Description <b>根据地址获得数据的字节流</b>
     */
    public static byte[] getFileBytesByURL(String strUrl) {
    	if(strUrl==null) return null;
    	
        try {
            URL url = new URL(strUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5 * 1000);
            InputStream inStream = conn.getInputStream();//通过输入流获取文件数据
            byte[] btFile = readInputStream(inStream);//得到文件的二进制数据
            return btFile;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    
     /**
     * @author  luohuida
     * @time    Mar 7, 2015
     * @param inStream 输入流
     * @return
     * @throws Exception
     * @Description <b>从输入流中获取文件数据</b>
     */
    public static byte[] readInputStream(InputStream inStream) throws Exception {
    	if(null==inStream) return null;
    	
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len;
        while ((len = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        inStream.close();
        return outStream.toByteArray();
    }
    
    
     /**
	 * @author  luohuida
	 * @time    Mar 7, 2015
	 * @param img 文件数据流
	 * @param fileName 文件保存时的名称
	 * @Description <b>将文件写入到磁盘</b>
	 */
    public static String writeFileToDisk(byte[] fileBt, String fileName) {
    	if(null==fileBt || null==fileName) return null;
    	
       try {
           File file = new File(ACVSystemConst.STORAGE_PATH + fileName);
           FileOutputStream fops = new FileOutputStream(file);
           fops.write(fileBt);
           fops.flush();
           fops.close();
          System.out.println("文件已经写入");
          return file.getPath();
       } catch (Exception e) {
           e.printStackTrace();
           return null;
       }
   }
    
    /**
	 * @author  luohuida
	 * @time    Mar 7, 2015
	 * @param img 文件数据流
	 * @param fileName 文件保存时的名称
	 * @Description <b>将文件写入到磁盘</b>
	 */
    public static void saveFile(byte[] fileBt, String fileName) {
    	if(null==fileBt || null==fileName) return;
    	
       try {
           File file = new File("classpath*:/apk/" + fileName);
           FileOutputStream fops = new FileOutputStream(file);
           fops.write(fileBt);
           fops.flush();
           fops.close();
          System.out.println("文件已经写入");
       } catch (Exception e) {
           e.printStackTrace();
       }
   }

    
    
}
